﻿#region - 版 本 注 释 -
/*----------------------------------------------------------------
 * 版权所有 (c) 2024   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-GLJ2Q8J
 * 公司名称：
 * 命名空间：AutoCAD.EntityTool
 * 唯一标识：cd35dde5-61e6-44d3-b65c-7a302677ae89
 * 文件名：ShowMessageTools
 * 当前用户域：DESKTOP-GLJ2Q8J
 * 
 * 创建者：Guo-79991
 * 电子邮箱：799919859@qq.com
 * 创建时间：2024/7/15 12:04:57
 * 版本：V1.0.0
 *
 * 功能描述(Description):
 * 参考文档(Reference):
 * 数据表(Tables):
 * 作者(Author):Guo
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion - 版 本 注 释 -


using Color = Autodesk.AutoCAD.Colors.Color;

namespace AutoCAD.EntityTools
{
    public static class TextTools
    {
        /// <summary>
        /// 在指定位置插入文字（支持颜色和字号）
        /// </summary>
        /// <param name="position">插入点坐标</param>
        /// <param name="colorIndex">颜色索引(ACI颜色)</param>
        /// <param name="text">文字内容</param>
        /// <param name="textHeight">文字高度</param>
        /// <param name="layerName">图层名（可选）</param>
        /// <returns>插入的文字ObjectId</returns>
        public static ObjectId InsertText(this Document doc, 
            Point3d position,
            short colorIndex,
            string text,
            double textHeight,
            string layerName = "0")
        {
            //Database db = HostApplicationServices.WorkingDatabase;
            Database db = doc.Database; ;
            ObjectId textId = ObjectId.Null;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 获取块表
                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                // 打开模型空间
                BlockTableRecord btr = trans.GetObject(
                    bt[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;

                // 创建文字对象
                DBText dbText = new DBText
                {
                    Position = position,
                    TextString = text,
                    Height = textHeight,
                    Color = Color.FromColorIndex(ColorMethod.ByAci, colorIndex)
                };

                // 设置图层（如果存在）
                if (!string.IsNullOrEmpty(layerName))
                {
                    LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    if (lt.Has(layerName))
                        dbText.Layer = layerName;
                }

                // 添加到模型空间
                btr.AppendEntity(dbText);
                trans.AddNewlyCreatedDBObject(dbText, true);
                textId = dbText.ObjectId;

                trans.Commit();
            }
            return textId;
        }

        /// <summary>
        /// 批量插入文字（高性能版）
        /// </summary>
        public static List<ObjectId> InsertTexts(this Document doc,
            IEnumerable<Point3d> positions,
            short colorIndex,
            IEnumerable<string> texts,
            double textHeight)
        {
            var ids = new List<ObjectId>();
            //var db = HostApplicationServices.WorkingDatabase;
            var db = doc.Database;

            using (var trans = db.TransactionManager.StartTransaction())
            {
                var bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                var btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                var pairs = positions.Zip(texts, (pos, txt) => new { pos, txt });
                foreach (var pair in pairs)
                {
                    var txt = new DBText
                    {
                        Position = pair.pos,
                        TextString = pair.txt,
                        Height = textHeight,
                        Color = Color.FromColorIndex(ColorMethod.ByAci, colorIndex)
                    };
                    btr.AppendEntity(txt);
                    trans.AddNewlyCreatedDBObject(txt, true);
                    ids.Add(txt.ObjectId);
                }
                trans.Commit();
            }
            return ids;
        }





        /// <summary>
        /// 在指定位置插入文字（支持颜色和字号）
        /// </summary>
        /// <param name="location"></param>
        /// <param name="msg"></param>
        /// <param name="layerName"></param>
        /// <param name="colorIndex"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static MText InsertText(this Point3d location, string msg, string layerName, int colorIndex = 4, double height = 350)
        {
            //AddLayer(lt, "窗信息");  layerId is null ;
            MText text = new MText()
            {
                Contents = msg,
                TextHeight = height,
                Location = location,

                ColorIndex = colorIndex,
                Layer = layerName   //先图层创建
            };
            return text;
        }



        /// <summary>
        /// 在指定位置插入文字（支持颜色和字号）
        /// </summary>
        /// <param name="location"></param>
        /// <param name="i"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static MText ShowText(this Point3d location, int i, double height = 80)
        {
            string[] InFo = new[] { "输入内容：", "长：1200", "高：2100", "底高度：900", "上底高度：800", "层高：3600", "窗台：", "窗型：", "其他...", "...", "...", "输入内容：" };
            //AddLayer(lt, "窗信息");  layerId is null ;
            MText text = new MText()
            {
                Contents = InFo[i],
                TextHeight = height,
                Location = location - Vector3d.XAxis * 600,

                ColorIndex = 8,
                //Layer = "窗信息"   //先图层创建
            };
            return text;
        }
    }
}
